package cn.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.jws.WebService;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 @Bean
    PasswordEncoder passwordEncoder(){
        return  new BCryptPasswordEncoder();
 }

 @Override
 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication().withUser("admin")
             .password(passwordEncoder().encode("123456"))
             .roles("admin").and()
             .withUser("sang").password(passwordEncoder().encode("123456"))
             .roles("user");
 }

 @Override
 public  void  configure(WebSecurity  web){
     web.ignoring().antMatchers("/jquery-1.12.4.js/**","/test/**");
 }

 @Override
protected  void  configure(HttpSecurity  http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/add/**","/toAdd/**").hasRole("admin")
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .permitAll()
            .and().csrf().disable();
     }
}
